package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.*;
import cn.com.thinvent.dispatch.entity.*;
import cn.com.thinvent.dispatch.entity.request.TaskDTO;
import cn.com.thinvent.dispatch.entity.response.*;
import cn.com.thinvent.dispatch.service.biz.ModelTaskService;
import cn.com.thinvent.dispatch.service.biz.util.DataBaseUtil;
import cn.com.thinvent.dispatch.service.biz.util.DataConvertUtils;
import cn.com.thinvent.dispatch.service.biz.util.HttpClientUtil;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class ModelTaskServiceImpl implements ModelTaskService {

    @Resource
    private ModelTaskMapper modelTaskMapper;

    @Resource
    private ModelTaskTargetMappingMapper modelTaskTargetMappingMapper;

    @Resource
    private ModelTargetMapper modelTargetMapper;

    @Resource
    private ModelDatasourceMapper modelDatasourceMapper;

    @Resource
    private ModelResultMapper modelResultMapper;

    @Resource
    private FileContentMapper fileContentMapper;

    @Resource
    private ModelDatasourceResultMappingMapper modelDatasourceResultMappingMapper;

    @Resource
    private ModelDatasourceMappingMapper modelDatasourceMappingMapper;

    @Resource
    private ModelErrorMapper modelErrorMapper;

    @Override
    public PagedGridResult<ModelTaskResponse> list(Integer userId, String taskName, Integer pageNum, Integer pageSize) {
        PagedGridResult<ModelTaskResponse> pagedGridResult = new PagedGridResult<>();
        Integer pageBegin = (pageNum - 1) * pageSize;
        List<ModelTask> modelTasks = modelTaskMapper.queryAllByLimit(userId, taskName, pageBegin, pageSize);
        Long count = modelTaskMapper.count(userId, taskName);
        List<ModelTaskResponse> modelTaskResponses = new ArrayList<>();
        DataConvertUtils.convertDOList2DTOList(modelTasks, modelTaskResponses, ModelTaskResponse.class);
        pagedGridResult.setRows(modelTaskResponses);
        pagedGridResult.setRecords(count);
        pagedGridResult.setTotal((Integer.parseInt(count.toString()) + pageSize - 1 )/ pageSize);
        pagedGridResult.setPage(pageNum);
        // fill 数据源id信息
        modelTaskResponses.stream().forEach(modelTaskResponse -> {
            List<ModelDatasourceMapping> modelDatasourceMappings = modelDatasourceMappingMapper.queryByTaskId(modelTaskResponse.getId());
            List<ModelDatasourceMapping> modelDatasourceMappings2 = modelDatasourceMappingMapper.queryByTaskIdUn(modelTaskResponse.getId());

            if(!CollectionUtils.isEmpty(modelDatasourceMappings)) {
                List<Integer> collect = modelDatasourceMappings.stream().map(ModelDatasourceMapping::getDatasourceId).collect(Collectors.toList());
                List<String> fileName = modelDatasourceMappings.stream().map(ModelDatasourceMapping::getFileName).collect(Collectors.toList());

                String join = StringUtils.join(collect, ",");
                modelTaskResponse.setDatasourceIds(join);
                modelTaskResponse.setSourceIds(collect);
                modelTaskResponse.setFileNames(fileName);
            }
            if(!CollectionUtils.isEmpty(modelDatasourceMappings2)){
                List<Integer> collect = modelDatasourceMappings2.stream().map(ModelDatasourceMapping::getDatasourceId).collect(Collectors.toList());
                String join = StringUtils.join(collect, ",");
                modelTaskResponse.setDatasourceIds(join);
                modelTaskResponse.setSourceIds(collect);
            }
        });
        return pagedGridResult;
    }

    @Override
    public ModelTask saveOrUpdate(TaskDTO taskDTO) {
        Date date = new Date();
        if(taskDTO.getId() == null) {
            // 新增任务信息
            ModelTask modelTask = DataConvertUtils.convertDTO2DO(taskDTO, new ModelTask());
            modelTask.setCreateTime(date);
            modelTask.setUpdateTime(date);
            modelTaskMapper.insert(modelTask);
            // 新增任务和条件映射信息
            List<ModelTarget> modelTargets = taskDTO.getModelTargets();
            if(!CollectionUtils.isEmpty(modelTargets)) {
                modelTargets.stream().forEach(modelTarget -> {
                    ModelTaskTargetMapping modelTaskTargetMapping = new ModelTaskTargetMapping();
                    modelTaskTargetMapping.setTargetId(modelTarget.getId());
                    modelTaskTargetMapping.setTaskId(modelTask.getId());

                    modelTaskTargetMapping.setCreateTime(date);
                    modelTaskTargetMapping.setUpdateTime(date);
                    modelTaskTargetMappingMapper.insert(modelTaskTargetMapping);
                });
            }
            // 新增任务和数据源之间的映射
            List<Integer> datasourceIds = taskDTO.getDatasourceIds();
            if(!CollectionUtils.isEmpty(datasourceIds)) {
                datasourceIds.stream().forEach(dasourceId -> {
                    ModelDatasourceMapping modelDatasourceMapping = new ModelDatasourceMapping();
                    modelDatasourceMapping.setTaskId(modelTask.getId());
                    modelDatasourceMapping.setDatasourceId(dasourceId);
                    Date d = new Date();
                    modelDatasourceMapping.setCreateTime(d);
                    modelDatasourceMapping.setUpdateTime(d);
                    modelDatasourceMappingMapper.insert(modelDatasourceMapping);
                });
            }
            return modelTask;
        } else {
            // 修改任务信息
            ModelTask modelTask = DataConvertUtils.convertDTO2DO(taskDTO, new ModelTask());
            modelTaskMapper.update(modelTask);
            // 删除映射关系
            modelTaskTargetMappingMapper.deleteByTaskId(modelTask.getId());
            // 新增映射关系
            List<ModelTarget> modelTargets = taskDTO.getModelTargets();
            if(!CollectionUtils.isEmpty(modelTargets)) {
                modelTargets.stream().forEach(modelTarget -> {
                    ModelTaskTargetMapping modelTaskTargetMapping = new ModelTaskTargetMapping();
                    modelTaskTargetMapping.setTargetId(modelTarget.getId());
                    modelTaskTargetMapping.setTaskId(modelTask.getId());
                    modelTaskTargetMapping.setCreateTime(date);
                    modelTaskTargetMapping.setUpdateTime(date);
                    modelTaskTargetMappingMapper.insert(modelTaskTargetMapping);
                });
            }
            // 删除任务和结构化数据源之间的映射
            modelDatasourceMappingMapper.deleteByTaskId(taskDTO.getId());
            // 新增任务和结构化数据源之间的映射
            List<Integer> datasourceIds = taskDTO.getDatasourceIds();
            if(!CollectionUtils.isEmpty(datasourceIds)) {
                datasourceIds.stream().forEach(datasourceId -> {
                    ModelDatasourceMapping modelDatasourceMapping = new ModelDatasourceMapping();
                    modelDatasourceMapping.setTaskId(modelTask.getId());
                    modelDatasourceMapping.setDatasourceId(datasourceId);
                    Date d = new Date();
                    modelDatasourceMapping.setCreateTime(d);
                    modelDatasourceMapping.setUpdateTime(d);
                    modelDatasourceMappingMapper.insert(modelDatasourceMapping);
                });
            }
            return modelTask;
        }
    }

    @Override
    public Integer delete(Integer taskId) {
        // 删除任务
        int delete = modelTaskMapper.deleteById(taskId);
        // 删除任务和条件映射
        modelTaskTargetMappingMapper.deleteByTaskId(taskId);
        return delete;
    }

    @Override
    public void actionNow(Integer taskId, Integer userId) {
        // 查询任务信息
        ModelTask modelTask = modelTaskMapper.queryById(taskId);
        // 查询执行条件信息
        List<ModelTaskTargetMapping> modelTaskTargetMappings = modelTaskTargetMappingMapper.getListByTaskId(taskId);
        List<ModelTarget> modelTargets = null;
        if(!CollectionUtils.isEmpty(modelTaskTargetMappings)) {
            List<Integer> targetIds = modelTaskTargetMappings.stream().map(ModelTaskTargetMapping::getTargetId).collect(Collectors.toList());
            modelTargets = modelTargetMapper.getListByIds(targetIds);
        }
        if(null != modelTask) {
            if("结构化数据源".equals(modelTask.getSourceType())) {
                // 查询任务和数据源映射
                List<ModelDatasourceMapping> modelDatasourceMappings = modelDatasourceMappingMapper.queryListByTaskId(taskId);
                if(!CollectionUtils.isEmpty(modelDatasourceMappings)) {
                    for(ModelDatasourceMapping modelDatasourceMapping : modelDatasourceMappings) {
                        // 查询数据源信息
                        ModelDatasource modelDatasource = modelDatasourceMapper.queryById(modelDatasourceMapping.getDatasourceId());
                        final String[] content = {""};
                        // 根据数据源配置去查询结果集
                        try {
                            List<Map<String, Object>> fieldValues = DataBaseUtil.getFieldValues(modelDatasource, 0, 0);
                            // 使用数据源信息发送请求到模型 并且记录结果
                            for(Map<String, Object> fieldValue : fieldValues) {
                                fieldValue.forEach((k, v) -> {
                                    content[0] = content[0] + v;
                                });
                                // 发送http请求到模型
                                List example = modelTargets.stream().map(ModelTarget::getExample).collect(Collectors.toList());
                                Map<String, String> map = new HashMap<>();
                                map.put("prompt", content[0]+example);
                                System.out.println("----------------------" +content[0]+example);
                                String s = JSONObject.toJSONString(map);
                                String postString = HttpClientUtil.doPost("http://60.204.204.87:80",s);
                                // 解析返回的字符串
                                ChatResponse chatResponse = JSON.parseObject(postString, ChatResponse.class);
                                // 保存结果到数据库
                                ModelResult modelResult = new ModelResult();
                                modelResult.setResult(chatResponse.getResponse());
                                modelResult.setTaskId(taskId);
                                modelResult.setUserId(userId);
                                Date date = new Date();
                                modelResult.setCreateTime(date);
                                modelResult.setUpdateTime(date);
                                modelResultMapper.insert(modelResult);
                                // 保存任务和数据源和结果之间的映射
                                ModelDatasourceResultMapping modelDatasourceResultMapping = new ModelDatasourceResultMapping();
                                modelDatasourceResultMapping.setDatasourceId(modelDatasource.getId());
                                modelDatasourceResultMapping.setResultId(modelResult.getId());
                                modelDatasourceResultMapping.setDataKeyword(content[0]);
                                modelDatasourceResultMapping.setTaskId(taskId);
                                Date d = new Date();
                                modelDatasourceResultMapping.setCreateTime(d);
                                modelDatasourceResultMapping.setUpdateTime(d);
                                modelDatasourceResultMappingMapper.insert(modelDatasourceResultMapping);
                            }
                        } catch (Throwable throwable) {
                            ModelError modelError = new ModelError();
                            modelError.setTaskId(taskId);
                            modelError.setDatasourceId(modelDatasourceMapping.getDatasourceId());
                            modelError.setRequestParam(content[0]);
                            modelError.setMessage(throwable.getMessage());
                            Date date = new Date();
                            modelError.setCreateTime(date);
                            modelError.setUpdateTime(date);
                            modelErrorMapper.insert(modelError);
                        }
                    }
                }
            }
            if("非结构化数据源".equals(modelTask.getSourceType())) {
                List<Long> ids = new ArrayList<>();
                // 查询任务和数据源映射
                List<ModelDatasourceMapping> modelDatasourceMappings = modelDatasourceMappingMapper.queryByTaskId(taskId);
                if(!CollectionUtils.isEmpty(modelDatasourceMappings)) {
                    // fileContent的外键列表
                    List<Integer> datasourceIds = modelDatasourceMappings.stream().map(ModelDatasourceMapping::getDatasourceId).collect(Collectors.toList());
                    for(Integer datasourceId : datasourceIds) {
                        ids.add(Long.parseLong(datasourceId.toString()));
                    }
                }
                List<FileContent> fileContents = fileContentMapper.queryByFileids(ids);
                for(FileContent fileContent : fileContents) {
                    String s = null;
                    try {
                        // 发送http请求到模型
                        List example = modelTargets.stream().map(ModelTarget::getExample).collect(Collectors.toList());
                        Map<String, String> map = new HashMap<>();
                        map.put("prompt", fileContent.getContent());
                        s = JSONObject.toJSONString(map);
                        String postString = HttpClientUtil.doPost("http://60.204.204.87:80",s);
                        // 解析返回的字符串
                        ChatResponse chatResponse1 = JSON.parseObject(postString, ChatResponse.class);

                        Map<String, Object> map1 = new HashMap<>();
                        map1.put("prompt", example.toString());
                        map1.put("history", chatResponse1.getHistory());
                        s = JSONObject.toJSONString(map1);
                        String postString1 = HttpClientUtil.doPost("http://60.204.204.87:80",s);
                        // 解析返回的字符串
                        ChatResponse chatResponse = JSON.parseObject(postString1, ChatResponse.class);

                        // 保存结果到数据库
                        ModelResult modelResult = new ModelResult();
                        modelResult.setResult(chatResponse.getResponse());
                        System.out.println("----------------------" +chatResponse.getResponse());
                        modelResult.setTaskId(taskId);
                        modelResult.setUserId(userId);
                        Date date = new Date();
                        modelResult.setCreateTime(date);
                        modelResult.setUpdateTime(date);
                        modelResultMapper.insert(modelResult);

                        // 保存任务和数据源和结果之间的映射
                        ModelDatasourceResultMapping modelDatasourceResultMapping = new ModelDatasourceResultMapping();
                        modelDatasourceResultMapping.setDatasourceId(fileContent.getFileid());
                        modelDatasourceResultMapping.setResultId(modelResult.getId());
                        modelDatasourceResultMapping.setDataKeyword(fileContent.getContent());
                        modelDatasourceResultMapping.setTaskId(taskId);
                        Date d = new Date();
                        modelDatasourceResultMapping.setCreateTime(d);
                        modelDatasourceResultMapping.setUpdateTime(d);
                        modelDatasourceResultMappingMapper.insert(modelDatasourceResultMapping);
                    } catch (Throwable throwable) {
                        ModelError modelError = new ModelError();
                        modelError.setTaskId(taskId);
                        modelError.setDatasourceId(fileContent.getFileid());
                        modelError.setRequestParam(s);
                        modelError.setMessage(throwable.getMessage());
                        Date date = new Date();
                        modelError.setCreateTime(date);
                        modelError.setUpdateTime(date);
                        modelErrorMapper.insert(modelError);
                    }
                }
            }
        }
        // 修改任务执行状态
        modelTask.setStatus("已执行");
        modelTaskMapper.update(modelTask);
    }

    @Override
    public ActionResultResponse actionResult(Integer taskId) {
        ActionResultResponse actionResultResponse = new ActionResultResponse();
        // 查询任务信息
        ModelTask modelTask = modelTaskMapper.queryById(taskId);
        actionResultResponse.setModelTask(modelTask);
        // 查询数据源信息
        if(null != modelTask) {
            if("结构化数据源".equals(modelTask.getSourceType())) {
                // 根据taskId查询映射信息
                List<ModelDatasourceResultMapping> modelDatasourceResultMappings = modelDatasourceResultMappingMapper.queryByTaskId(taskId);
                if(!CollectionUtils.isEmpty(modelDatasourceResultMappings)) {
                    ModelDatasourceResultMapping modelDatasourceResultMapping = modelDatasourceResultMappings.get(0);
                    // 查询数据源信息
                    ModelDatasource modelDatasource = modelDatasourceMapper.queryById(modelDatasourceResultMapping.getDatasourceId());
                    ModelDatasourceResponse modelDatasourceResponse = DataConvertUtils.convertDO2DTO(modelDatasource, new ModelDatasourceResponse());
                    actionResultResponse.setModelDatasourceResponse(modelDatasourceResponse);
                    // 查询数据源和结果映射信息
                    actionResultResponse.getModelDatasourceResponse().setModelDatasourceResultMapping(modelDatasourceResultMapping);
                    // 查询执行结果信息
                    ModelResult modelResult = modelResultMapper.queryById(modelDatasourceResultMapping.getResultId());
                    actionResultResponse.getModelDatasourceResponse().setModelResult(modelResult);
                }
            }
            if("非结构化数据源".equals(modelTask.getSourceType())) {
                // 根据taskId查询映射信息
                List<ModelDatasourceResultMapping> modelDatasourceResultMappings = modelDatasourceResultMappingMapper.queryByTaskId(taskId);
                if(!CollectionUtils.isEmpty(modelDatasourceResultMappings)) {
                    ModelDatasourceResultMapping modelDatasourceResultMapping = modelDatasourceResultMappings.get(0);
                    // 查询数据源信息
                    FileContent fileContent = fileContentMapper.queryById(modelDatasourceResultMapping.getDatasourceId());
                    FileContentResponse fileContentResponse = DataConvertUtils.convertDO2DTO(fileContent, new FileContentResponse());
                    actionResultResponse.setFileContent(fileContentResponse);

                    // 查询数据源和结果映射信息
                    actionResultResponse.getFileContent().setModelDatasourceResultMapping(modelDatasourceResultMapping);

                    // 查询执行结果信息
                    ModelResult modelResult = modelResultMapper.queryById(modelDatasourceResultMapping.getResultId());
                    actionResultResponse.getFileContent().setModelResult(modelResult);
                }
            }
        }
        return actionResultResponse;
    }

    @Override
    public ResultDetailResponse resultDetail(Integer taskId, Integer pageNum, Integer pageSize) {
        ResultDetailResponse response = new ResultDetailResponse();
        // 模型任务详情信息
        ModelTask modelTask = modelTaskMapper.queryById(taskId);
        response.setModelTask(modelTask);
        // 数据源信息
        // 查询数据源信息
        if(null != modelTask) {
            if("结构化数据源".equals(modelTask.getSourceType())) {
                // 查询数据源信息
                List<ModelDatasourceMapping> modelDatasourceMappings = modelDatasourceMappingMapper.queryByTaskId(taskId);
                if(!CollectionUtils.isEmpty(modelDatasourceMappings)) {
                    List<Integer> datasourceIds = modelDatasourceMappings.stream().map(ModelDatasourceMapping::getDatasourceId).collect(Collectors.toList());
                    List<ModelDatasource> modelDatasources = modelDatasourceMapper.queryByIds(datasourceIds);
                    response.setModelDatasources(modelDatasources);
                }
                // 分页查询数据源结果映射列表
                PagedGridResult<ModelDatasourceResultMappingResponse> modelDatasourceResultMappingResponse = getResultPage(taskId, pageNum, pageSize);
                response.setModelDatasourceResultMappingResponse(modelDatasourceResultMappingResponse);
            }
            if("非结构化数据源".equals(modelTask.getSourceType())) {
                // 查询数据源信息
                List<ModelDatasourceMapping> modelDatasourceMappings = modelDatasourceMappingMapper.queryByTaskId(taskId);
                if(!CollectionUtils.isEmpty(modelDatasourceMappings)) {
                    List<Integer> datasourceIds = modelDatasourceMappings.stream().map(ModelDatasourceMapping::getDatasourceId).collect(Collectors.toList());
                    List<Long> ids = new ArrayList<>();
                    for(Integer datasourceId : datasourceIds) {
                        ids.add(Long.parseLong(datasourceId.toString()));
                    }
                    List<FileContent> fileContents = fileContentMapper.queryByFileids(ids);
                    response.setFileContents(fileContents);
                    // 分页查询数据源结果映射列表
                    PagedGridResult<ModelDatasourceResultMappingResponse> modelDatasourceResultMappingResponse = getResultPage(taskId, pageNum, pageSize);
                    response.setModelDatasourceResultMappingResponse(modelDatasourceResultMappingResponse);
                }
            }
        }
        // 执行错误信息
        List<ModelError> modelErrorList = modelErrorMapper.queryByTaskId(taskId);
        if(!CollectionUtils.isEmpty(modelErrorList)) {
            response.setModelErrorList(modelErrorList);
        }
        return response;
    }

    @Override
    public void resActionNow(Integer taskId, Integer integer) {
        // 删除映射
        modelDatasourceResultMappingMapper.deleteByTaskId(taskId);
        // 删除结果信息
        modelResultMapper.deleteByTaskId(taskId);
        actionNow(taskId,integer);
    }

    @Override
    public void continueActionNow(Integer taskId, Integer userId) {
        List<ModelError> modelErrors = modelErrorMapper.queryByTaskId(taskId);
        modelErrors.stream().forEach(modelError -> {
            Map<String, String> map = new HashMap<>();
            map.put("prompt", modelError.getRequestParam());
            String s = JSONObject.toJSONString(map);
            String postString = HttpClientUtil.doPost("http://60.204.204.87:80",s);
            // 解析返回的字符串
            ChatResponse chatResponse = JSON.parseObject(postString, ChatResponse.class);
            if(!"200".equals(chatResponse.getStatus().toString())){
                return;
            }
            // 保存结果到数据库
            ModelResult modelResult = new ModelResult();
            modelResult.setResult(chatResponse.getResponse());
            modelResult.setTaskId(taskId);
            modelResult.setUserId(userId);
            Date date = new Date();
            modelResult.setCreateTime(date);
            modelResult.setUpdateTime(date);
            modelResultMapper.insert(modelResult);
            // 保存任务和数据源和结果之间的映射
            ModelDatasourceResultMapping modelDatasourceResultMapping = new ModelDatasourceResultMapping();
            modelDatasourceResultMapping.setDatasourceId(modelError.getDatasourceId());
            modelDatasourceResultMapping.setResultId(modelResult.getId());
            modelDatasourceResultMapping.setDataKeyword(modelError.getRequestParam());
            modelDatasourceResultMapping.setTaskId(taskId);
            Date d = new Date();
            modelDatasourceResultMapping.setCreateTime(d);
            modelDatasourceResultMapping.setUpdateTime(d);
            modelDatasourceResultMappingMapper.insert(modelDatasourceResultMapping);
            //删除错误信息
            modelErrorMapper.deleteById(modelError.getId());
        });

    }

    @Override
    public PagedGridResult<ModelResult> resultPageList(Integer taskId, Integer pageNum, Integer pageSize) {
        PagedGridResult<ModelResult> pagedGridResult = new PagedGridResult<>();
        Integer pageBegin = (pageNum - 1) * pageSize;
        List<ModelResult> modelResults = modelResultMapper.queryAllByLimit(taskId, pageBegin, pageSize);
        Long count = modelResultMapper.count(taskId);
        pagedGridResult.setRows(modelResults);
        pagedGridResult.setRecords(count);
        pagedGridResult.setTotal((Integer.parseInt(count.toString()) + pageSize - 1 )/ pageSize);
        pagedGridResult.setPage(pageNum);
        return pagedGridResult;
    }

    @Override
    public ResultPageDetailResponse resultPageDetail(Integer taskId, Integer resultId) {
        ResultPageDetailResponse response = new ResultPageDetailResponse();
        // 查询任务内容结果映射表
        List<ModelDatasourceResultMapping> modelDatasourceResultMappings = modelDatasourceResultMappingMapper.queryByTaskId(taskId);
        Map<Integer, ModelDatasourceResultMapping> map = modelDatasourceResultMappings.stream().collect(Collectors.toMap(
                ModelDatasourceResultMapping::getResultId,
                obj -> obj,
                (key1, key2) -> key1
        ));
        ModelDatasourceResultMapping modelDatasourceResultMapping = map.get(resultId);
        String dataKeyword = modelDatasourceResultMapping.getDataKeyword();

        ModelResult modelResult = modelResultMapper.queryById(resultId);

        response.setSourceContent(dataKeyword);
        response.setTaskId(taskId);
        response.setResultId(resultId);
        response.setResult(modelResult.getResult());

        return response;
    }

    @Override
    public void updateResult(Integer resultId, String result) {
        ModelResult modelResult = new ModelResult();
        modelResult.setId(resultId);
        modelResult.setResult(result);
        modelResultMapper.update(modelResult);
    }

    private PagedGridResult<ModelDatasourceResultMappingResponse> getResultPage(Integer taskId, Integer pageNum, Integer pageSize) {
        PagedGridResult<ModelDatasourceResultMappingResponse> modelDatasourceResultMappingResponse = new PagedGridResult<>();
        Integer pageBegin = (pageNum - 1) * pageSize;
        List<ModelDatasourceResultMapping> modelDatasourceResultMappings = modelDatasourceResultMappingMapper.queryAllByLimit(taskId, pageBegin, pageSize);
        Long count = modelDatasourceResultMappingMapper.count(taskId);
        List<ModelDatasourceResultMappingResponse> modelDatasourceResultMappingResponses = new ArrayList<>();
        DataConvertUtils.convertDOList2DTOList(modelDatasourceResultMappings, modelDatasourceResultMappingResponses, ModelDatasourceResultMappingResponse.class);
        modelDatasourceResultMappingResponse.setRows(modelDatasourceResultMappingResponses);
        modelDatasourceResultMappingResponse.setRecords(count);
        modelDatasourceResultMappingResponse.setTotal((Integer.parseInt(count.toString()) + pageSize - 1 )/ pageSize);
        modelDatasourceResultMappingResponse.setPage(pageNum);
        List<Integer> resultIds = modelDatasourceResultMappingResponses.stream().map(ModelDatasourceResultMappingResponse::getResultId).collect(Collectors.toList());
        List<ModelResult> modelResults = modelResultMapper.queryByIds(resultIds);
        if(!CollectionUtils.isEmpty(modelResults)) {
            Map<Integer, ModelResult> map = modelResults.stream().collect(Collectors.toMap(
                    ModelResult::getId,
                    obj -> obj,
                    (key1, key2) -> key1
            ));
            modelDatasourceResultMappingResponses.stream().forEach(modelDatasourceResultMappingResponse1 -> {
                modelDatasourceResultMappingResponse1.setModelResult(map.get(modelDatasourceResultMappingResponse1.getResultId()));
            });
        }
        return modelDatasourceResultMappingResponse;
    }
}
